<template>
  <div class="login-register-wrap">
    <div class="wrap">
      <h1 class="title">XBQ</h1>
      <div
        class="closeLogin"
        @click="$emit('close')"
      >
        <span class="iconfont icon-guanbi"></span>
      </div>
      <div class="choose-button">
        <button
          class="btn login-btn"
          :class ="{active:currTem==='login'}"
          @click="currTem = 'login'"
        >登录</button>
        <button
          class="btn register-btn"
          :class ="{active:currTem==='register'}"
          @click = "currTem = 'register'"
        >注册</button>
      </div>
      <!-- 用来放置 login 或者 register -->
      <component
        :is = "currTem"
        @switch="toggle"
        @login = "$emit('login')"
        @close="$emit('close')"
      ></component>
    </div>
  </div>
</template>

<script>
  import login from "./components/login"
  import register from "./components/register"
  // <!-- 还有引入validatir -->
  import { Validator } from 'vee-validate';
  // 修改默认
  const dictionary = {
    zh_CN: {
      messages: {
        required: () => '该值不能为空哦'
      }
    }
  };
  Validator.updateDictionary(dictionary);
  // username
  Validator.extend('username', {
    messages: {
      zh_CN:field => '邮箱号输入错误'
    },
    validate: value => {
      return /^((1\d{10})|((\w-*\.*)+@(\w-?)+(\.\w{2,})+))$/.test(value);
    }
  });
  // password
  Validator.extend('password', {
    messages: {
      zh_CN:field => '请输入8至11位的中、英文字符'
    },
    validate: value => {
      return /^.[0-9A-Za-z]{7,10}$/.test(value);
    }
  });
  // confirmPassword
  Validator.extend('confirmPassword', {
    messages: {
      zh_CN:field => '密码不一致'
    },
    validate: (value, args) => {
      return args == value;
    }
  });
  // check
  Validator.extend('checkCode', {
    messages: {
      zh_CN:field => '验证码输入错误'
    },
    validate: (value,args) => {
      return value == args;
    }
  });

  export default {
    name: 'loginRegister',
    props : {
      sonTarget : {
        default : 'login',
        type : String
      }
    },
    data() {
      return {
        currTem : this.sonTarget
      }
    },
/*    computed : {
      getCurrTem : function () {
        if (!this.sonTarget) {
          return this.currTem;
        }
        if (this.sonTarget !== currTem) {
          return
        }
      }
    },*/
    components : {
      login : login,
      register : register
    },
    methods : {
      toggle : function (value) {
        this.$data.currTem = value
      }
    }
  }
</script>

<style scoped>
  @import '../../../../../assets/css/loginRegister.css';
  .login-register-wrap{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0,0,0,.3);
    z-index: 500;
  }

  .wrap {
    overflow: hidden;
    margin: 30px auto;
    width: 260px;
    border:  1px solid rgba(69, 72, 75, 0.15);
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    padding: 10px 20px;
    background: white;
    position: relative;
  }

  /* title */
  .title {
    color: #75b3cc;
    font: bold 30px/30px "simhei","微软雅黑";
    width: 100%;
    text-align: center;
  }

  .closeLogin {
    position: absolute;
    right: 0;
    width: 30px;
    height: 30px;
    line-height: 30px;
    top: 0;
    text-align: center;
    color: #b2bac2;
  }
  .closeLogin span:hover{
    color: #007fff;
    cursor: pointer;
  }
  .choose-button {
    margin-top: 20px;
    width: 100%;
    -webkit-border-radius: 5p;
    -moz-border-radius: 5px;
    border-radius: 5px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    overflow: hidden;
    margin-bottom: 20px;
  }
  .choose-button .btn {
    border: none;
    outline: none;
    padding: 0;
    margin: 0;
    width:50%;
    float: left;
    font: 16px/30px "simhei","华文新魏";
    height: 30px;
    background: #dddddd;
  }
  .choose-button .btn:hover {
    background-color: #cecece;
    cursor: pointer;
  }
  .login-btn {
    -webkit-border-radius: 5px 0 0 5px;
    -moz-border-radius: 5px 0 0 5px;
    border-radius: 5px 0 0 5px;
  }
  .register-btn {
    -webkit-border-radius: 0 5px 5px 0;
    -moz-border-radius: 0 5px 5px 0;
    border-radius: 0 5px 5px 0;
  }
  .choose-button .active {
    background: #007fff;
  }
  .choose-button .active:hover {
    background: #0071e2;
    cursor: pointer;
  }
</style>
